var app = angular.module('myApp', ['ngRoute','ngCookies']);

app.factory('authInterceptor', function($rootScope,  $cookies){
    return {
        request: function(config){
            config.headers = config.headers || {};
            if($cookies.get('ZJD-Token')){
                config.headers['ZJD-Token'] = $cookies.get('ZJD-Token');
            }
            return config;
        },
        responseError: function(response){
            // ...
        }
    };
});

app.config(['$routeProvider', '$httpProvider',
    function ($routeProvider, $httpProvider) {
        $routeProvider.when('/home', {
            templateUrl: 'view/home/home.html',
            controller: 'homeCtrl'
        }).when('/home/speedState', {
            templateUrl: 'view/speedState.html',
            controller: 'speedStateCtrl'
        }).when('/home/workList', {
            templateUrl: 'view/workList.html',
            controller: 'workListCtrl'
        }).when('/home/investigation', {
            templateUrl: 'view/investigation.html',
            controller: 'investigationCtrl'
        }).when('/home/registerBook', {
            templateUrl: 'view/registerBook.html',
            controller: 'registerBookCtrl'
        }).when('/home/aggrege', {
            templateUrl: 'view/aggrege.html',
            controller: 'aggregeCtrl'
        }).when('/home/stage', {
            templateUrl: 'view/stage.html',
            controller: 'stageCtrl'
        }).when('/login', {
            templateUrl: 'login.html'
        }).otherwise({
            redirectTo: '/home'
        });

        $httpProvider.defaults.transformRequest = function (obj) {
            var str = [];
            for (var p in obj) {
                str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
            }
            return str.join("&");
        };
        $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8';
        $httpProvider.interceptors.push('authInterceptor');
    }]);

app.run(function ($rootScope, $window, $location, $log, $cookies, $http) {
    var userid = $cookies.get('userid');
    // var userid = 'a02099afec4c4dffb467a6a6143aaa70';
    if(userid == null){
        $location.path("/login");
    }
    $http.post("http://120.24.219.92:8899/findmytowns", {
        userid: userid
    })
        .then(function (result) {
            if(result==null){
                $location.path("/login");
            }
            var msg = result.data.msg;
            if(msg == "没有找到该用户"){
                $location.path("/login");
            }
        });

    var locationChangeStartOff = $rootScope.$on('$locationChangeStart', locationChangeStart);
    var locationChangeSuccessOff = $rootScope.$on('$locationChangeSuccess', locationChangeSuccess);

    var routeChangeStartOff = $rootScope.$on('$routeChangeStart', routeChangeStart);
    var routeChangeSuccessOff = $rootScope.$on('$routeChangeSuccess', routeChangeSuccess);

    function locationChangeStart(event) {

    }

    function locationChangeSuccess(event) {

    }

    function routeChangeStart(event) {

    }

    function routeChangeSuccess(event, rou) {

        var path = rou.$$route.originalPath;
        console.log(path);
        var paths = ['/home', '/home/speedState', '/home/workList', '/home/investigation', '/home/registerBook', '/home/aggrege', '/home/stage'];
        for (var i = 0; i < paths.length; i++) {
            if (path == paths[i]) {
                $rootScope.activeId = i;
                if (i < 4) {
                    $(".index-header-nav-tab ").animate({scrollLeft: i * 78}, {speed: 500, easing: 'linear'})
                } else {
                    $(".index-header-nav-tab ").animate({scrollLeft: 410}, {speed: 500, easing: 'linear'})
                    // $(".index-header-nav-tab ").scrollLeft(410);
                }
            }
        }
    }
});

app.controller('myCtrl', function ($scope, $http, $location, $rootScope, $cookies) {

    // var userid = 'a02099afec4c4dffb467a6a6143aaa70';
    var userid = $cookies.get('userid');

    $scope.ShoworHide = false;
    $scope.tabNav = [
        {
            name: '综合情况',
            id: 1,
            isShow: true
        },
        {
            name: '进度指标',
            id: 2,
            isShow: true
        },
        {
            name: '工作组织',
            id: 3,
            isShow: true
        },
        {
            name: '权籍调查',
            id: 4,
            isShow: true
        },
        {
            name: '编辑成图',
            id: 5,
            isShow: true
        },
        {
            name: '每日上传',
            id: 6,
            isShow: false
        },
        {
            name: '完成情况',
            id: 7,
            isShow: false
        }
    ];
    $http.post("http://120.24.219.92:8899/findmytowns", {
        userid: userid
    })
        .then(function (result) {
            var msg = result.data.msg;
            console.log(msg);
            if (msg==null){
                $location.path("/login");
            }
            if(result.data.code == 400){
                $scope.ShoworHide = false;
            }else if(result.data.code == 401){
                $scope.ShoworHide = true;
                $scope.tabNav[5].isShow = false;
                $scope.tabNav[6].isShow = false;
            }else if(result.data.code == 0){
                $scope.ShoworHide = true;
                $scope.tabNav[5].isShow = true;
                $scope.tabNav[6].isShow = true;
            }
        });

    $rootScope.activeId = 0;
    $scope.tabClick = function (index) {
        $rootScope.activeId = index;
        if (index === 0) {
            $location.path("/home");
        }
        if (index === 1) {
            $location.path("/home/speedState");
        }
        if (index === 2) {
            $location.path("/home/workList");
        }
        if (index === 3) {
            $location.path("/home/investigation");
        }
        if (index === 4) {
            $location.path("/home/registerBook");
        }
        if (index === 5) {
            $location.path("/home/aggrege");
        }
        if (index === 6) {
            $location.path("/home/stage");
        }
    }

});
